package com.youbug.common.config;

import com.youbug.common.interceptor.AuthInterceptor;
import com.youbug.mall.cart.interceptor.CartInterceptor;
import com.youbug.mall.order_oms.interceptor.OrderInterceptor;
import com.youbug.mall.seckill.interceptor.SeckillInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(authInterceptor);

        registry.addInterceptor(new CartInterceptor()).addPathPatterns("/mall/cart/**").addPathPatterns("/mall/member/**");

        registry.addInterceptor(new OrderInterceptor()).addPathPatterns("/mall/order/**");

        registry.addInterceptor(new SeckillInterceptor()).addPathPatterns("/mall/seckill/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")// 允许所有路径跨域
                .allowedOrigins("*")// 允许所有origin跨域
                .allowedMethods("*")// 所有方法
                .allowedHeaders("*")
                .allowCredentials(true);// 允许携带凭证
    }

    /**
     * webSocket 服务端
     * @return
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}
